/*
    Project:        Home Power Control
    File:           HPCRainGauge.h
    Author:         christian@karunamusic.at
    Date:           02.2015
    Description:    Sensor to measure the amount of rain in l/m2.
                    The sensor gives one tick per 0,2794mm water in the sensor.
                    1mm is aequivalent to 1 l/m2.
 */

#ifndef HPCRAINGAUGE_H
#define HPCRAINGAUGE_H

#include <QTimer>
#include <QMutex>
#include <QString>
#include <HPCSensor.h>

/** \brief Sensor to measure the amount of rain.
 *  Gives the amount of rain in liter/m2.
 *  Count the ticks of the sensor (via interrupt) in the given intervall-time.
 */
class HPCRainGauge : public HPCSensor
{
    Q_OBJECT
public:
    /** \brief Constructor
     *
     * \param name name of the sensor.
     * \param id database id of the sensor.
     * \param pin GPIO pin number.
     * \param interval time (in seconds) to pause between querying the sensor.
     */
    explicit HPCRainGauge( const QString& name, quint32 id, quint8 pin, quint16 interval );
    /** \brief Destructor.
     */
    virtual ~HPCRainGauge();

    inline quint16 Interval() const
        { return m_Interval; }

    inline void Interval( quint16 i )
        { m_Interval = i; }

    /** \brief Get class with content as printable string.
     *
     * \return string
     */
    QString asString() const;

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

private slots:
    void calcAverage();

private:
    /** \brief Interrupt service routine to count ticks from the sensor.
     */
    static void isr();

    static QMutex m_Mutex;
    static unsigned int m_Counter; /**< count the ticks of the sensor in the given time. */
    quint16 m_Interval; /**< interval time */
    QTimer *m_Timer; /**< the timer for calculating the average value */
};

#endif // HPCRAINGAUGE_H
